package cn.edu.swu.bc.servlet;

import cn.edu.swu.bc.filter.AuthFilter;
import cn.edu.swu.bc.utils.JsonUtils;
import cn.edu.swu.bc.utils.DBUtils;
import cn.edu.swu.bc.entity.User;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.Writer;
import java.sql.SQLException;

public class UserServlet extends HttpServlet {

    private static final long serialVersionUID = 1148763434511986230L;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        String isOthers="TRUE";
        String isAdmin= (String) session.getAttribute(AuthFilter.IS_ADMIN);
        User user = null;
        try {
            if(request.getParameter("id").equals("undefined"))
            {
                user = DBUtils.GetUser((Integer) session.getAttribute(AuthFilter.USER_ID));
                isOthers="FALSE";
            }
            else
                user= DBUtils.GetUser(Integer.parseInt(request.getParameter("id")) );
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        Writer writer = response.getWriter();
        writer.write(JsonUtils.userToJson(user,isOthers));
    }


}
